﻿using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using HtmlAgilityPack;

class WebCrawler
{
    private HttpClient client = new HttpClient();
    private ConcurrentDictionary<string, ConcurrentBag<string>> phoneNumbers = new ConcurrentDictionary<string, ConcurrentBag<string>>();
    private ConcurrentBag<string> crawledUrls = new ConcurrentBag<string>();

    public async Task StartCrawlingAsync(string keyword)
    {
        // 使用搜索引擎搜索关键字
        string searchUrl = $"https://www.bing.com/search?q={keyword}+高校+电话";
        var response = await client.GetAsync(searchUrl);
        var content = await response.Content.ReadAsStringAsync();

        // 解析搜索结果页面，获取链接
        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(content);
        var links = doc.DocumentNode.SelectNodes("//a[@href]");

        // 使用多线程并行处理每个链接
        Parallel.ForEach(links, async link =>
        {
            string href = link.GetAttributeValue("href", string.Empty);
            await ProcessUrlAsync(href);
        });

        // 显示已爬取的URL和电话号码
        foreach (var url in crawledUrls)
        {
            Console.WriteLine($"Crawled URL: {url}");
            if (phoneNumbers.TryGetValue(url, out var numbers))
            {
                foreach (var number in numbers)
                {
                    Console.WriteLine($"Phone Number: {number}");
                }
            }
        }
    }

    private async Task ProcessUrlAsync(string url)
    {
        // 避免重复爬取相同的URL
        if (!crawledUrls.Contains(url))
        {
            crawledUrls.Add(url);
            var response = await client.GetAsync(url);
            var content = await response.Content.ReadAsStringAsync();

            // 使用正则表达式查找电话号码
            Regex phoneRegex = new Regex(@"\b\d{3}[-.]?\d{3}[-.]?\d{4}\b");
            MatchCollection matches = phoneRegex.Matches(content);

            foreach (Match match in matches)
            {
                // 记录电话号码和对应的URL
                phoneNumbers.AddOrUpdate(url, new ConcurrentBag<string> { match.Value }, (key, oldValue) =>
                {
                    oldValue.Add(match.Value);
                    return oldValue;
                });
            }
        }
    }
}

class Program
{
    static async Task Main(string[] args)
    {
        WebCrawler crawler = new WebCrawler();
        Console.WriteLine("Enter keyword to search:");
        string keyword = Console.ReadLine();
        await crawler.StartCrawlingAsync(keyword);
    }
}
